/*
 * system.c
 *
 *  Created on: Apr 2, 2012
 *      Author: Tri_D
 */
#include "system.h"
#include "serial.h"
#include "sma.h"
#include "math.h"
#include "key.h"

Sys_t Sys;

extern BOOL Collect_SMA_Pkg(que_512 * pQ, SMA_Pkg_t * pS);
extern void Process_SMA(SMA_Pkg_t *pS, uint8_t * pd, uint16_t * pAnnuc, uint8_t * pMode, uint8_t * pUnit, uint8_t * pcolor, Bat_t * pBat, BOOL * pUpdate);

void Timer_Init(Time_t * pT)
{
	pT->bOn = 0;				// This flag indicates keeping track of the provided time
	pT->hour =0;
	pT->min = 0;
	pT->q_sec = 0;
	pT->sec = 0;	
}

void Sys_Init(Sys_t * pS)
{
	Ser_Init(&pS->Ser);
	Timer_Init(&pS->Time);
	init_keybrd(&pS->keybrd);
	(void) memset(pS->Color, 100, 3);
	(void) memset(pS->Str, 0, 9);
	(void) memset(pS->Unit, 0, 3);
	pS->bRdy = 0;
	// Initialize greeting message	
	(void) memcpy(pS->Str, "200PLC", 6);
	pS->Bat = NO_BAT;
	pS->Annuc = 0;
	pS->Mode = ' ';
	(void) memcpy(pS->Unit, "   ", 3);
	pS->bUpdate = TRUE;
	
}


void Sys_Reset(Sys_t * pS)
{	
	(void) memcpy(Sys.Str, "200PLC", 6);
	Sys.Bat = NO_BAT;
	Sys.Annuc = 0;
	Sys.Mode = ' ';
	(void) memcpy(Sys.Unit, "   ", 3);	
	pS->Time.bOn = 0;
	pS->bUpdate = TRUE;
}


void delay_250ms(void)
{
	volatile uint8_t c = Sys.Time.q_sec;
	while(c == Sys.Time.q_sec);	
}

void delay_500ms(void)
{
	delay_250ms();
	delay_250ms();
}

void delay_1sec(void)
{
	volatile uint8_t c = Sys.Time.sec;
	while(c == Sys.Time.sec);
}

void delay_sec(uint8_t sec)
{
	uint8_t c;
	for(c = 0; c <sec; c++)
		delay_1sec();
}


uint8_t Read_Timer_Q_Sec(Time_t * pT)
{
	return pT->q_sec;
}

uint8_t Read_Timer_Sec(Time_t * pT)
{
	return pT->sec;
}


// convert ascii string to 8 bit value
uint8_t Ascii2Val(uint8_t *psrc, uint8_t size)
{
	uint8_t res_ = 0;
	uint8_t c = 0, c1 = 0;
	
	for(c = 0; c < size; c++)
	{
		c1 = *(psrc+c);
		if (c1 >= '0' && c1 <= '9')
		{
			c1 -= '0';
			res_ += (c1 * (uint8_t) pow(10, size-1-c));
		}
		else
		{
			res_ =0;
			break;
		}
	}
	return res_;
}

// convert an unsigned 8 bit value into ascii string
void Val2Ascii(uint8_t val, uint8_t *pdes)
{
	uint8_t c = 0;
	uint8_t c2 = 0;
	if(val >= 100)
	{
		c2 = val/100;
		*(pdes+c) = c2 + '0';
		val = val - c2*100;
		c++;
	}
	
	if(val == 0)
	{
		*(pdes+1) = '0';
		*(pdes+2) = '0';
		return;
	}
	else if (val >= 10)
	{
		c2 = val/10;
		*(pdes+c) = c2+'0';
		val = val - c2*10;
		c++;
	}
	
	if(val == 0)
	{
		*(pdes+c) = '0';
		return;
	}
	else 
	{
		*(pdes+c) = val+'0';
	}	
}


void Process_Serial_Data(Serial_t * pS, Time_t * pT)
{
	BOOL res_ = FALSE;
	SMA_Pkg_t pP;
	uint8_t timeout_ = 0;
	SMA_Init(&pP);
	
	timeout_ = Read_Timer_Q_Sec(pT);
	while(timeout_ == pT->q_sec && res_ == FALSE)
		res_ = Collect_SMA_Pkg(&pS->Rx, &pP);
	if (res_ == TRUE)
	{
		Process_SMA(&pP, Sys.Str, &Sys.Annuc, &Sys.Mode, Sys.Unit, Sys.Color, &Sys.Bat, &Sys.bUpdate);
		pT->bOn = MAX_SERIAL_TIME_OUT;											// timer starts running
	}
}


void process_keybrd_intent(void)
{
		switch(Sys.keybrd.Intent)
		{
			case ZERO_INTENT:
			{
				Tx_Ser(&Sys.Ser, "\x0aZ\x0d", 3);
			}
			break;
			
			case TARE_INTENT:
			{
				Tx_Ser(&Sys.Ser, "\x0aT\x0d", 3);
			}
			break;
			
			case NET_INTENT:
			{
				Tx_Ser(&Sys.Ser, "\x0aNet\x0d", 5);
			}
			break;
			
			
			case UNIT_INTENT:
			{
				Tx_Ser(&Sys.Ser, "\x0aUnit\x0d", 6);
			}
			break;
			
			case FN_INTENT:
			{
				Tx_Ser(&Sys.Ser, "\x0a\x46unc\x0d", 6);
			}
			break;
			
			case PRINT_INTENT:
			{
				Tx_Ser(&Sys.Ser, "\x0aXG\x0d", 4);
			}
			break;
			
			case SETUP_INTENT:
			{
				Tx_Ser(&Sys.Ser, "\x0aSetup\x0d", 7);
			}
			break;		
			
			case CLEAR_TARE_INTENT:
			{
				Tx_Ser(&Sys.Ser, "\x0a\x43\x0d", 3);
			}
			break;
			
			case PRINT_TARE_INTENT:
			{
				Tx_Ser(&Sys.Ser, "\x0aM\x0d", 3);
			}
			break;
			
			default:
			break;
		}		
		Sys.keybrd.Intent = NO_INTENT;
}
